//
//  MCMineViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/1.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMineViewController.h"
#import "MCMineHeaderView.h"
#import "MCMineTableviewCell.h"
#import "MCMineCellModel.h"
#import "MCSettingViewController.h"
#import "MCMineTitleModel.h"
#import "MCBankcardManageViewController.h"
#import "MCSecurityCenterViewController.h"
#import "MCRechargeViewController.h"
#import "MCPersonInformationViewController.h"
#import "MCTopUpRecordViewController.h"
#import "MCWithdrawRecordViewController.h"
#import "MCGameRecordViewController.h"
#import "MCWithdrawRecDeltailViewController.h"
#import "UIImage+Extension.h"
#import "MCMineInfoModel.h"
#import "MCUserMoneyModel.h"
#import "MCHasPayPwdModel.h"
#import "MCMineAddBankCardViewController.h"
#import "MCPasswordSettingViewController.h"

@interface MCMineViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
MCMineHeaderViewDelegate,
MCMineCellDelegate
>
/**headerView*/
@property (nonatomic,strong)MCMineHeaderView * headerView;
/**tableView*/
@property(nonatomic, strong)UITableView *tableView;
/**sectionMarr*/
@property(nonatomic, strong)NSMutableArray*sectionMarr;
/**titleMarr*/
@property(nonatomic, strong)NSMutableArray*titleMarr;

/*
 * Api数据模型
 */
/**获取用户详情*/
@property (nonatomic,strong) MCMineInfoModel *mineInfoModel;
/**查询用户余额及冻结金额*/
@property (nonatomic,strong) MCUserMoneyModel *userMoneyModel;
/**查询用户是否已设置资金密码*/
@property (nonatomic,strong) MCHasPayPwdModel *hasPayPwdModel;



@end

@implementation MCMineViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
    [self loadData];
    
 
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];


    /*
     * 每次进入这个页面   都重新刷新
     */
    [self refreshData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    MCPaySLBaseModel * dataSourceModel=[MCPaySLBaseModel sharedMCPaySLBaseModel];
    [dataSourceModel removeDataSource];
}
#pragma mark==================setProperty======================
-(void)setProperty{
    self.view.backgroundColor=RGB(239, 246, 253);
    self.navigationController.navigationBarHidden=YES;
    _sectionMarr= [[NSMutableArray alloc]init];
}

#pragma mark==================createUI======================
-(void)createUI{
    /*
     *
     */
    _headerView=[[MCMineHeaderView alloc]init];
    [self.view addSubview:_headerView];
    _headerView.delegate=self;
    _headerView.frame=CGRectMake(0, 0, G_SCREENWIDTH, 200);
   
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];

    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerView.mas_bottom).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);

    }];
}
#pragma mark==================loadData======================
-(void)loadData{
    NSMutableArray * marr=[[NSMutableArray alloc]init];
    for(int i=0 ;i<4; i++){
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = [NSString stringWithFormat:@"%@--%d",NSStringFromClass([MCMineTableviewCell class]),i];
        model.className=NSStringFromClass([MCMineTableviewCell class]);
        model.height = [MCMineTableviewCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        /*
         * 传递参数
         */
        model.userInfo = self.titleMarr[i];
        
        [marr addObject:model];
    }
    SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr];
    model0.headerhHeight=0.0001;
    model0.footerHeight=10;
    [_sectionMarr addObject:model0];
    [_tableView reloadData];

    
}

-(void)refreshData{
    __weak __typeof(self)wself = self;
    
    MCMineInfoModel *mineInfoModel = [MCMineInfoModel sharedMCMineInfoModel];
    self.mineInfoModel = mineInfoModel;
    if (mineInfoModel.UserRealName) {
        wself.headerView.dataSource=wself.mineInfoModel;
    }
    
        [mineInfoModel refreashDataAndShow];
        mineInfoModel.callBackSuccessBlock = ^(id manager) {
            
            wself.mineInfoModel=[MCMineInfoModel mj_objectWithKeyValues:manager];
            
            wself.headerView.dataSource=wself.mineInfoModel;
            
        };

    
    
    MCUserMoneyModel *userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
    self.userMoneyModel=userMoneyModel;
    if (userMoneyModel.FreezeMoney&&userMoneyModel.LotteryMoney) {
        //可用余额
        wself.headerView.lab_accountBalance.text=[NSString stringWithFormat:@"可用余额:%@",[MCMathUnits GetMoneyShowNum:userMoneyModel.LotteryMoney]];

        //冻结金额
        wself.headerView.lab_frozenAccount.text=[NSString stringWithFormat:@"冻结金额:%@",[MCMathUnits GetMoneyShowNum:userMoneyModel.FreezeMoney]];
        
    }
    
    [userMoneyModel refreashDataAndShow];
    userMoneyModel.callBackSuccessBlock = ^(id manager) {
        
        //可用余额
        wself.headerView.lab_accountBalance.text=[NSString stringWithFormat:@"可用余额:%@",[MCMathUnits GetMoneyShowNum:manager[@"LotteryMoney"]]];
        wself.userMoneyModel.LotteryMoney=manager[@"LotteryMoney"];
        //冻结金额
        wself.headerView.lab_frozenAccount.text=[NSString stringWithFormat:@"冻结金额:%@",[MCMathUnits GetMoneyShowNum:manager[@"FreezeMoney"]]];
        wself.userMoneyModel.FreezeMoney=manager[@"FreezeMoney"];
    };
    
    MCHasPayPwdModel * hasPayPwdModel=[MCHasPayPwdModel sharedMCHasPayPwdModel];
    [hasPayPwdModel refreashDataAndShow];
    self.hasPayPwdModel=hasPayPwdModel;
    hasPayPwdModel.callBackSuccessBlock = ^(id manager) {
        wself.hasPayPwdModel.PayOutPassWord=[NSString stringWithFormat:@"%@",manager[@"PayOutPassWord"]];
    };

    

}
#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _sectionMarr[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCMineTableviewCell class])]) {
        MCMineTableviewCell *ex_cell=(MCMineTableviewCell *)cell;
        ex_cell.delegate=self;
//        typeof(self) weakSelf = self;
//        ex_cell.collectionViewCellDidSelectedBlock = ^(CollectionModel *model){
//         
//             
//        
//        };
        ex_cell.dataSource=cm.userInfo;
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
 
}
#pragma mark==================MCMineHeaderViewDelegate======================
/*
 * 跳转设置界面
 */
-(void)goToSettingViewController{
    
    MCSettingViewController * vc=[[MCSettingViewController alloc]init];
    self.navigationController.navigationBarHidden=NO;
    [self.navigationController pushViewController:vc animated:YES];
    
}

/*
 * 跳转充值界面
 */
-(void)goToRechargeViewController{
    
    if ([self.mineInfoModel.UserType intValue] ==1) {
        [SVProgressHUD showInfoWithStatus:@"试玩账号不能充值!"];
        return;
    }
    
    
    MCRechargeViewController * vc = [[MCRechargeViewController alloc]init];

    [self.navigationController pushViewController:vc animated:YES];
    
}

/*
 * 跳转提现
 */
-(void)goToWithdrawViewController{

    if ([self.mineInfoModel.UserType intValue] ==1) {
        [SVProgressHUD showInfoWithStatus:@"试玩账号不能提款!"];
        return;
    }
    BOOL hasCard = [self.mineInfoModel.BankCardNumber length]>1?YES:NO;
    BOOL hasPassword = [self.hasPayPwdModel.PayOutPassWord intValue]==1?YES:NO;
    
    /*
     * 情况一： 无资金密码  无默认银行卡
     */
    if (!hasCard&&!hasPassword) {
      
        //先绑卡-》设置资金密码-》退回我的
        MCMineAddBankCardViewController * vc=[[MCMineAddBankCardViewController alloc]init];
        vc.type=NOPasswordAndNOCard;
        self.navigationController.navigationBarHidden=NO;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    //情况二： 有资金密码  无默认银行卡
    }else if (hasPassword&&!hasCard){
     
        //先绑卡-》退回我的
        MCMineAddBankCardViewController * vc=[[MCMineAddBankCardViewController alloc]init];
        vc.type=HasPasswordAndNOCard;
        self.navigationController.navigationBarHidden=NO;
        [self.navigationController pushViewController:vc animated:YES];
        
    //情况三： 无资金密码  有默认银行卡
    }else if (!hasPassword&&hasCard){
     
        //设置资金密码-》退回我的
        MCPasswordSettingViewController * vc =[[MCPasswordSettingViewController alloc]init];
        self.navigationController.navigationBarHidden=NO;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    //情况四： 有资金密码  有默认银行卡
    }else{
        
        MCWithdrawRecDeltailViewController * vc=[[MCWithdrawRecDeltailViewController alloc]init];
        self.navigationController.navigationBarHidden=NO;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    
}

/*
 * 跳转站内信
 */
-(void)goToMessageViewController{
    
}
/*
 * 跳转修改用户名
 */
-(void)goToModifyUserNameViewController{
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(NSMutableArray *)titleMarr{
    if (!_titleMarr) {
        _titleMarr =[[NSMutableArray alloc]init];
        MCMineTitleModel * model0=[MCMineTitleModel getModelWithMainTitle:@"我的账户" andSubTitle:@[@"个人资料",@"银行卡管理",@"安全中心"]];
        [_titleMarr addObject:model0];
        MCMineTitleModel * model1=[MCMineTitleModel getModelWithMainTitle:@"财务中心" andSubTitle:@[@"充值记录",@"提款记录"]];
        [_titleMarr addObject:model1];
        MCMineTitleModel * model2=[MCMineTitleModel getModelWithMainTitle:@"订单报表" andSubTitle:@[@"游戏记录",@"追号记录",@"帐变记录",@"个人报表"]];
        [_titleMarr addObject:model2];
        MCMineTitleModel * model3=[MCMineTitleModel getModelWithMainTitle:@"团队报表" andSubTitle:@[@"开户中心",@"会员管理",@"契约管理",@"团队报表"]];
        [_titleMarr addObject:model3];
    }
    return _titleMarr;
}
#pragma mark-MCMineCellDelegate  /点击Cell跳转/
-(void)cellDidSelectWithType:(NSString *)type{
    
        UIViewController * vc = nil;
        if ([type isEqualToString:@"个人资料"]) {
        /*
        * 跳转个人资料
        */

        vc =[[MCPersonInformationViewController alloc]init];

        }else if ([type isEqualToString:@"银行卡管理"]){
            
        vc=[[MCBankcardManageViewController alloc]init];
            
        }else if ([type isEqualToString:@"安全中心"]){
            
        vc=[[MCSecurityCenterViewController alloc]init];
            
        }else if ([type isEqualToString:@"充值记录"]){
            
        vc=[[MCTopUpRecordViewController alloc]init];
            
        }else if ([type isEqualToString:@"提款记录"]){
          
        vc=[[MCWithdrawRecordViewController alloc]init];
            
        }else if ([type isEqualToString:@"游戏记录"]){
           
        MCGameRecordViewController * vc1 = [[MCGameRecordViewController alloc] init];
        vc = vc1;
        }else if ([type isEqualToString:@"追号记录"]){
            
        }else if ([type isEqualToString:@"帐变记录"]){

        }else if ([type isEqualToString:@"个人报表"]){
        }else if ([type isEqualToString:@"开户中心"]){
          
        }else if ([type isEqualToString:@"会员管理"]){

        }else if ([type isEqualToString:@"契约管理"]){
        }else if ([type isEqualToString:@"团队报表"]){
           
        }
    if (vc == nil) {
        return;
    }
    self.navigationController.navigationBarHidden=NO;
    [self.navigationController pushViewController:vc animated:YES];
}
@end










































